package springboot.springboot06_ssmp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springboot.common_utils.R;
import springboot.springboot06_ssmp.entity.Role;
import springboot.springboot06_ssmp.mapper.RoleMapper;
import springboot.springboot06_ssmp.service.IRoleService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author daiqiang
 * @since 2024-06-16
 */
@Slf4j
@CrossOrigin  //允许跨域
@RestController
@RequestMapping("role")
//@ApiModel(value = "Role对象", description = "")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private  RoleMapper roleMapper;
    @GetMapping
    public R getAll() {
        List<Role> resultList = roleService.list();
        return R.success(resultList);
    }
    @GetMapping("/list")
    public R<Map<String,Object>> getUserList(@RequestParam(value="roleName",required=false) String  roleName,
                                            // @RequestParam (value="card",required=false) String  card,
                                             @RequestParam (value="pageNo",required=false) Long  pageNo,
                                             @RequestParam (value="pageSize",required=false) Long  pageSize
    ){
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.hasLength(roleName),Role::getRoleName,roleName);
        //lqw.eq(StringUtils.hasLength(card),Role::getCard,card);

        Page<Role> page = new Page<>(pageNo,pageSize);
        roleMapper.selectPage(page,lqw);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());
        return R.success(data);

    }
    @GetMapping("{currentPage}/{pageSize}")
    public R getAllPage(@PathVariable int currentPage,@PathVariable int pageSize,Role role){
        System.out.println("参数----》"+role);
        IPage<Role> pageRole=roleService.getPage(currentPage,pageSize,role );

        return R.success(pageRole);

    }
    @PostMapping
    public R save(@RequestBody Role role) {
           //1.写入角色表
           //2.写入角色菜单关系表
        try {
            roleService.addRole(role);
            return R.success("success");
        } catch (Exception e) {

            return R.fail("fail");
        }
    }
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id) {
       Role  role= roleService.getRoleById(id);

        return  R.success(role);
    }
    @DeleteMapping("{id}")
    public  R delete(@PathVariable Integer id){
        roleService.deleteRoleById(id);
        return  R.success(roleService.removeById(id));
    }

    @PutMapping
    public  R update(@RequestBody Role role){
        //1.修改角色表
        //2.删除角色菜单关系表
        //3.写入角色菜单关系表
        roleService.updateRole(role);
            return  R.success("修改角色成功！");
    }
    @GetMapping("/all")
    public R <List<Role>> getAllRole() {
        List<Role>  roleList= roleService.list();
        return  R.success(roleList);
    }
}
